/*
* @file tts.h
* @brief Header file for the TTS (Text-to-Speech) class
* @author feiniao
* @version 1.0
* @date 2025-5-14
* @note This file contains the definition of the TTS class
*       which is responsible for handling text-to-speech functionality.
*/


#ifndef _TTS_H_
#define _TTS_H_

#include "Loudspeaker.h"
#include "cJSON.h"
#include "esp_heap_caps.h"
#include "esp_psram.h"
#include "esp_http_client.h"
#include "app/global_msg.h"

#define TAG "TTS"

#define TTS_URL "https://tsn.baidu.com/text2audio"
#define TTS_API_KEY "AGMFUDkOsmzha5H1b8ZAgds1"
#define TTS_SECRET_KEY "Ju1DSK2RQl0wjua3fz4CVyvujcLH3We6"
#define CUID "9zh2Ldb9IXsMdhOsjOdyid2KL6EcNxND"
#define MAX_TOKEN_RESPONSE_LEN 2048  // 定义足够大的缓冲区
#define MIN(a, b) ((a) < (b) ? (a) : (b))

class TTS {
private:
    Loudspeaker L;
    void fetch_token_tts();
    char* url_encode(const char* str);
    static esp_err_t tts_event_handler(esp_http_client_event_t* evt);
public:
    TTS();
    ~TTS();
    void demo_process();
    void process();
};

#endif

/*
use example
// tts test
void tts_task_test(void* arg) {
    TTS t;
    while (1) {
        t.demo_process();
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}

void wifi_task(void* arg) {
    MyWifi w;
    w.connect_wifi();
    vTaskDelay(pdMS_TO_TICKS(2000));
    while (1) {
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}

extern "C" void app_main(void) {
    xTaskCreatePinnedToCore(wifi_task, "wifi_task", 4096, NULL, 5, nullptr, 0);

    vTaskDelay(pdMS_TO_TICKS(2000));

    xTaskCreatePinnedToCore(tts_task_test, "tts_task_test", 8192, NULL, 5, nullptr, 0);
}
*/